#! /usr/bin/env bash

ROOTDIR=$(dirname "$0")/..
SBE_TOOL_VERSION=$(cat "$ROOTDIR"/version.txt)
SBE_JAR=$ROOTDIR/sbe-all/build/libs/sbe-all-$SBE_TOOL_VERSION.jar

[ -f $SBE_JAR ] || (echo "Missing $SBE_JAR. Run gradle first"; exit 1)

function usage {
    echo usage: $(basename "$0") [-d output_dir] -s schema
}

# defaults
OUTPUTDIR=.

while getopts "d:s:" OPT "$@"; do
    case $OPT in
    d)
        OUTPUTDIR=$OPTARG
        ;;
    s)
        SCHEMA=$OPTARG
        ;;
    *)
        echo "$OPT"
        usage
        exit 1
        ;;
    esac
done
shift $(($OPTIND - 1))

# Check args
[ -z "$SCHEMA" ] && (usage; exit 1)
[ -f "$SCHEMA" ] || (echo invalid schema "$SCHEMA"; exit 1)

java \
--add-opens java.base/jdk.internal.misc=ALL-UNNAMED \
-Dsbe.output.dir="$OUTPUTDIR" \
-Dsbe.generate.ir="false" \
-Dsbe.target.language="golang" \
-jar "$SBE_JAR" \
"$SCHEMA"
